import model from '../model/bullet'
import CanvasAbstract from './canvasAbstract'
import tank from './tank'
import player from './player'
import BulletModel from '../model/bullet'
import audio from '../service/audio'

class Bullet extends CanvasAbstract implements ICanvas {
  intervalId: number = 0
  num(): number {
    return 0
  }
  model(): BulletModelConstructor {
    return model
  }

  // 这里的render方法在main.ts文件中执行了
  render(): void {
    this.intervalId = setInterval(() => {
      this.createBullet()
      this.renderModels()
    }, 100)
  }

  createBullet() {
    tank.models.forEach((tank) => {
      const isExists = this.models.some((m) => m.tank === tank)
      if (!isExists) {
        this.models.push(new BulletModel(tank))
        // audio.fire()
      }
    })
  }

  // 添加玩家的子弹
  addPlayerBullet() {
    this.models.push(new BulletModel(player.models[0]))
    audio.fire()
  }

  stop() {
    clearInterval(this.intervalId)
  }
}

export default new Bullet('bullet')
